package com.yzf.enterprise.framework.web.domain;

import com.yzf.enterprise.common.enums.DeletedFlagEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 使用Mybatis TK插件通用服务实现层
 * <p>
 * Mapper : dao层mapper
 * T:继承了baseEntity的实体对象
 *
 * @author AKing31
 * @version 1.0
 * @since 2019-12-18 3:45 下午
 */
@Slf4j
public class BaseTkServiceImpl<Mapper, T extends BaseTkEntity> implements BaseTkService<T> {

    @Autowired
    public Mapper mapper;

    @Override
    public T selectByPrimaryKey(Object key) {
        try {
            Method selectByPrimaryKey = mapper.getClass().getMethod("selectByPrimaryKey", Object.class);
            return (T) selectByPrimaryKey.invoke(mapper, key);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("selectByPrimaryKey fail", e);
        }
        return null;
    }

    @Override
    public T selectOne(T param) {
        try {
            Method selectOne = mapper.getClass().getMethod("selectOne", Object.class);
            param.setDeleted(DeletedFlagEnum.NOT_DELETE.isValue());
            return (T) selectOne.invoke(mapper, param);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("selectOne fail", e);
        }
        return null;
    }

    @Override
    public List<T> select(T param) {
        try {
            param.setDeleted(DeletedFlagEnum.NOT_DELETE.isValue());
            Method select = mapper.getClass().getMethod("select", Object.class);
            return (List<T>) select.invoke(mapper, param);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("select fail", e);
        }
        return null;
    }

    @Override
    public int batchInsert(List<T> recordList) {
        try {
            Method insertList = mapper.getClass().getMethod("insertList", List.class);
            return (int) insertList.invoke(mapper, recordList);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("insertList fail", e);
        }
        return 0;
    }

    @Override
    public int insertSelective(T record) {
        try {
            Method insertSelective = mapper.getClass().getMethod("insertSelective", Object.class);
            return (int) insertSelective.invoke(mapper, record);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("insertSelective fail", e);
        }
        return 0;
    }

    @Override
    public int delete(T record) {
        try {
            Method delete = mapper.getClass().getMethod("delete", Object.class);
            return (int) delete.invoke(mapper, record);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("delete fail", e);
        }
        return 0;
    }

    @Override
    public int remove(T record) {
        record.setDeleted(DeletedFlagEnum.DELETED.isValue());
        return updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        try {
            Method updateByPrimaryKeySelective = mapper.getClass().getMethod("updateByPrimaryKeySelective", Object.class);
            return (int) updateByPrimaryKeySelective.invoke(mapper, record);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("updateByPrimaryKeySelective fail", e);
        }
        return 0;
    }

    @Override
    public int batchRemove(Long[] ids, String updateUser) {
        try {
            Method batchRemove = mapper.getClass().getMethod("batchRemove", Long[].class, String.class);
            return (int) batchRemove.invoke(mapper, ids, updateUser);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("batchRemove fail", e);
        }
        return 0;
    }

    @Override
    public int insertUseGeneratedKeys(T record) {
        try {
            Method insertSelective = mapper.getClass().getMethod("insertUseGeneratedKeys", Object.class);
            record.setDeleted(DeletedFlagEnum.NOT_DELETE.isValue());
            return (int) insertSelective.invoke(mapper, record);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            log.error("insertSelective fail", e);
        }
        return 0;
    }
}
